package com.hmkj.admin.controller.simulation;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.simulation.SubscribeModel;
import com.hmkj.core.po.simulation.Subscribe;
import com.hmkj.core.service.simulation.SubscribeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class SubscribeJsonController extends GenericController<Integer,Subscribe>{

    @Resource
    private SubscribeService subscribeService;

    /**
     * 高手列表json数据
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("simulation/user/json/list")
    public DtGrid userListJson(String dtGridPager) throws Exception {
        return subscribeService.getDtGridList(dtGridPager, SubscribeModel.class);
    }

    /**
     * 推荐发布
     */
    @ResponseBody
    @RequestMapping(value = "simulation/json/pubInfo")
    @RequiresPermissions("simulation:user:publish")
    public ResultEntity pubInfo(Integer id,String mark,Integer status) throws Exception {
        if(null == id || null == status || StringUtils.isBlank(mark)){
            throw new BussinessException("参数有误");
        }
        Subscribe subscribe = subscribeService.selectByPrimaryKey(id);
        if (null == subscribe) {
            throw new BussinessException("您要发布的内容不存在或已被删除");
        }
        subscribeService.updateByPrimaryKeySelective(new Subscribe(a -> {
            a.setId(subscribe.getId());
            switch (mark){
                case "high":
                    if(status == Subscribe.HIGHLEVEL.T1.code){
                        a.setHighLevel(Subscribe.HIGHLEVEL.T2.code);
                    }else if(status == Subscribe.HIGHLEVEL.T2.code){
                        a.setHighLevel(Subscribe.HIGHLEVEL.T1.code);
                    }
                    break;
                case "earn":
                    if(status == Subscribe.MOSTEARN.T1.code){
                        a.setMostEarn(Subscribe.MOSTEARN.T2.code);
                    }else if(status == Subscribe.MOSTEARN.T2.code){
                        a.setMostEarn(Subscribe.MOSTEARN.T1.code);
                    }
                    break;
                case "steady":
                    if(status == Subscribe.MOSTSTEADY.T1.code){
                        a.setMostSteady(Subscribe.MOSTSTEADY.T2.code);
                    }else if(status == Subscribe.MOSTSTEADY.T2.code){
                        a.setMostSteady(Subscribe.MOSTSTEADY.T1.code);
                    }
                    break;
                case "popular":
                    if(status == Subscribe.MOSTPOPULAR.T1.code){
                        a.setMostPopular(Subscribe.MOSTPOPULAR.T2.code);
                    }else if(status == Subscribe.MOSTPOPULAR.T2.code){
                        a.setMostPopular(Subscribe.MOSTPOPULAR.T1.code);
                    }
                    break;
            }
        }));
        return ok();
    }
}
